/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.dialogs;

import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.syncinfo.ResourceSyncInfo;
import net.sourceforge.perforce.ui.PerforceUIPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;

/**
  * @version $Revision: 20 $
  * @author	 <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class ConfirmationDialog extends Dialog 
{
	// ------------------------------------------------------- Private Constants
	
	private static final int LIST_ITEM_COUNT = 5;
	private static final String[] buttonLabels =
	{
		IDialogConstants.YES_LABEL, 
		IDialogConstants.NO_LABEL,
	};
	private static final int defaultButtonIndex = 0;

	// ------------------------------------------------------- Private Variables

	private String title;
	private String message;
	private IFile[] files;
	private String[] strings;
	private Button[] buttons;
	private Button detailsButton;
	private List list;
	private boolean listCreated = false;

	// --------------------------------------------------- Public Static Methods

	public static boolean openConfirmation(Shell parentShell, String title, 
		String message, IFile[] files)
	{
		ConfirmationDialog dialog = 
			new ConfirmationDialog(parentShell, title, message, files);
			
		return dialog.open() == 0;
	}

	public static boolean openConfirmation(Shell parentShell, String title, 
		String message, String[] files)
	{
		ConfirmationDialog dialog = 
			new ConfirmationDialog(parentShell, title, message, files);
			
		return dialog.open() == 0;
	}

	// ---------------------------------------------------- Private Constructors
	
	/** Constructor for ConfirmationDialog. */
	private ConfirmationDialog(Shell parentShell, String dialogTitle, 
		String message, IFile[] files)
	{
		super(parentShell);
		
		this.title = dialogTitle;
		this.message = message;
		this.files = files;
		setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);		
	}

	/** Constructor for ConfirmationDialog. */
	private ConfirmationDialog(Shell parentShell, String dialogTitle, 
		String message, String[] strings)
	{
		super(parentShell);
		
		this.title = dialogTitle;
		this.message = message;
		this.strings = strings;
		setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE | SWT.APPLICATION_MODAL);		
	}

	// ------------------------------------------------------- Protected Methods

	protected void buttonPressed(int id) 
	{
		if (id == IDialogConstants.DETAILS_ID) 
		{
			toggleDetailsArea();
		} 
		else 
		{
			super.buttonPressed(id);
		} 
	}

	protected void configureShell(Shell shell) 
	{
		super.configureShell(shell);
		shell.setText(title);
	}

	protected void createButtonsForButtonBar(Composite parent) 
	{
		buttons = new Button[buttonLabels.length];
		for (int i = 0; i < buttonLabels.length; i++) 
		{
			String label = buttonLabels[i];
			buttons[i] = 
				createButton(parent, i, label, defaultButtonIndex == i);
		}
		
		detailsButton = 
			createButton(parent, IDialogConstants.DETAILS_ID, 
				IDialogConstants.SHOW_DETAILS_LABEL, false);
	}
	
	protected Control createDialogArea(Composite parent) 
	{
		// create composite
		Composite composite = (Composite) super.createDialogArea(parent);
		((GridLayout) composite.getLayout()).numColumns = 2;

		// create image
		Image image = getImage();
		if (image != null) 
		{
			Label label = new Label(composite, 0);
			image.setBackground(label.getBackground());
			label.setImage(image);
			label.setLayoutData(
				new GridData(
					GridData.HORIZONTAL_ALIGN_CENTER |
					GridData.VERTICAL_ALIGN_BEGINNING));
		}

		// create message
		if (message != null) 
		{
			Label label = new Label(composite, SWT.WRAP);
			label.setText(message);

			GridData data =
				new GridData(
					GridData.GRAB_HORIZONTAL |
					GridData.GRAB_VERTICAL |
					GridData.HORIZONTAL_ALIGN_FILL |
					GridData.VERTICAL_ALIGN_CENTER);

			data.widthHint =
				convertHorizontalDLUsToPixels(
					IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH);

			label.setLayoutData(data);
			label.setFont(parent.getFont());
		}

		return composite;
	}

	protected Image getImage() 
	{
		return JFaceResources.getImageRegistry().get(DLG_IMG_QUESTION);
	}

	protected List createDropDownList(Composite parent) 
	{
		// create the list
		list = new List(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
	
		// fill the list
		populateList(list);
			
		GridData data = new GridData(
			GridData.HORIZONTAL_ALIGN_FILL | 
			GridData.GRAB_HORIZONTAL |
			GridData.VERTICAL_ALIGN_FILL | 
			GridData.GRAB_VERTICAL);
			
		data.heightHint = list.getItemHeight() * LIST_ITEM_COUNT;
		list.setLayoutData(data);
		listCreated = true;
		return list;
	}

	// --------------------------------------------------------- Private Methods

	private void populateList(List list) 
	{
		if (files != null)
		{
			for (int i = 0, length = files.length; i < length; i++)
			{
				try
				{
					IFile file = files[i];
					
					ResourceSyncInfo resourceSyncInfo =
						 PerforceCorePlugin.getSynchronizer().getResourceSync(file);
					
					list.add(resourceSyncInfo.getDepotFileName());
				}
				catch (PerforceException e)
				{
					PerforceUIPlugin.log(e.getStatus());
				}
			}
		}
		else
		{
			for (int i = 0, length = strings.length; i < length; i++)
			{
				list.add(strings[i]);
			}
		}
	}
	
	private void toggleDetailsArea() 
	{
		Point windowSize = getShell().getSize();
		Point oldSize = getContents().computeSize(SWT.DEFAULT, SWT.DEFAULT);
		
		if (listCreated) 
		{
			list.dispose();
			listCreated = false;
			detailsButton.setText(IDialogConstants.SHOW_DETAILS_LABEL);
		} 
		else 
		{
			list = createDropDownList((Composite)getContents());
			detailsButton.setText(IDialogConstants.HIDE_DETAILS_LABEL);
		}
	
		Point newSize = getContents().computeSize(SWT.DEFAULT, SWT.DEFAULT);
	
		getShell().setSize(
			new Point(windowSize.x, windowSize.y + (newSize.y - oldSize.y)));
	}
}
